package steed.util.digest;

import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import steed.exception.runtime.MessageRuntimeException;

public class RSAUtil {
	/**
	 * 加密算法RSA
	 */
	public static final String KEY_ALGORITHM = "RSA";
	
	/**
	 * 私钥加密
	 * @param data 要加密的数据
	 * @param privateKey 私钥
	 * @return 加密后的数据
	 */
	public static byte[] encryptByPrivateKey(byte[] data, String privateKey) {
		return doByPrivateKey(data, privateKey, Cipher.ENCRYPT_MODE);
	}
	/**
	 * 私钥解密
	 * @param encryptedData 要解密的数据
	 * @param privateKey 私钥
	 * @return 解密后的数据
	 */
	public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey) {
		return doByPrivateKey(encryptedData, privateKey, Cipher.DECRYPT_MODE);
	}
	
	private static byte[] doByPrivateKey(byte[] data, String privateKey,int opmode){
		byte[] keyBytes = getKeyBytes(privateKey);
		PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
		KeyFactory keyFactory;
		try {
			keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
			Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
			Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
			cipher.init(opmode, privateK);
			return cipher.doFinal(data);
		} catch (NoSuchAlgorithmException | InvalidKeySpecException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
			throw new MessageRuntimeException("rsa私钥加解密失败!",e);
		}
	}
	
	

	private static byte[] getKeyBytes(String key) {
		byte[] keyBytes = Base64Util.base64DecodeByte(key);
		return keyBytes;
	}

	/**
	 * 公钥加密
	 * @param data 要加密的数据
	 * @param privateKey 公钥
	 * @return 加密后的数据
	 */
    public static byte[] encryptByPublicKey(byte[] data, String publicKey){
        return doByPublicKey(data, publicKey, Cipher.ENCRYPT_MODE);
    }
    /**
	 * 公钥解密
	 * @param encryptedData 要解密的数据
	 * @param privateKey 公钥
	 * @return 解密后的数据
	 */
    public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey){
    	return doByPublicKey(encryptedData, publicKey, Cipher.DECRYPT_MODE);
    }
    
    private static byte[] doByPublicKey(byte[] data, String publicKey,int opmode){
        byte[] keyBytes = getKeyBytes(publicKey);
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory;
		try {
			keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
			Key publicK = keyFactory.generatePublic(x509KeySpec);
			Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
			cipher.init(opmode, publicK);
			
			return cipher.doFinal(data);
		} catch (NoSuchAlgorithmException | InvalidKeySpecException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
			throw new MessageRuntimeException("rsa公钥加解密失败!",e);
		}
    }
}
